package com.sugon.controller.sys;

import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.ApkVersionEntity;
import com.sugon.service.ApkVersionService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * APK版本Controller
 *
 * @author sugon
 * @date 2020-04-21 11:52:46
 */
@Controller
@RequestMapping("apkversion")
public class ApkVersionController {
    @Autowired
    private ApkVersionService apkVersionService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("apkversion:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ApkVersionEntity> apkVersionList = apkVersionService.queryList(query);
        int total = apkVersionService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(apkVersionList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("apkversion:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ApkVersionEntity apkVersion = apkVersionService.queryObject(id);

        return R.ok().put("apkVersion", apkVersion);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("apkversion:save")
    @ResponseBody
    public R save(@RequestBody ApkVersionEntity apkVersion) {
        // 根据应用版本名称设置版本号
        if (StrUtil.isNotEmpty(apkVersion.getVersion())) {
            apkVersion.setVno(Integer.parseInt(StrUtil.replace(apkVersion.getVersion(), ".", "")));
        }
        apkVersionService.save(apkVersion);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("apkversion:update")
    @ResponseBody
    public R update(@RequestBody ApkVersionEntity apkVersion) {
        // 根据应用版本名称设置版本号
        if (StrUtil.isNotEmpty(apkVersion.getVersion())) {
            apkVersion.setVno(Integer.parseInt(StrUtil.replace(apkVersion.getVersion(), ".", "")));
        }
        apkVersionService.update(apkVersion);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("apkversion:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        apkVersionService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ApkVersionEntity> list = apkVersionService.queryList(params);

        return R.ok().put("list", list);
    }
}
